<?php
/**
 * Core_Personal_EmailChangeForm
 * 
 * @author naspeh
 * @version $Id: EmailChangeForm.php 670 2008-06-30 10:24:28Z naspeh $
 */
class Core_Personal_EmailChangeForm extends Core_Form 
{
    /**
     * Инициализация
     *
     * @return Core_Personal_EmailChangeForm
     */    
    public function init()
    {
        $this->addElement('text', 'new_email', array(
            'required' => true,
            'label'    => 'Новый email:',
            'description' => 'Требует активации',
            'attribs'  => array(
                'class' => 'text span-4',
            ),     
            'validators' => array(
                new Zend_Validate_EmailAddress(),
                new Core_Validate_EmailExist(true)
            )
        ));
        $this->addElement('button', 'submit', array(
            'type'     => 'submit',
            'label'    => 'Сохранить',
            'attribs'  => array(
                'class' => 'positive',
                'icon'  => 'tick'
            )
        ));
        $defaults = array(
            'new_email'    => Core_Auth::get('new_email'),
        );
        $this->setDefaults($defaults);        
        return $this;
    }
    /**
     * Запускает форму на выполнение
     *
     * @return Core_Personal_EmailChangeForm
     */    
    public function process()
    {
        $rolesTable = new Core_Table_Roles();
        $values = $this->getValues(true);
        $data = array();
        if (!empty($values['new_email']) && $values['new_email'] != Core_Auth::get('email')) {
            $newEmail = $values['new_email'];
            $data['new_email'] = $newEmail;
            $codeConfirm = Core_Auth::getHash($newEmail);
            $data['code_confirm'] = $codeConfirm;
            Core_Informant::run($newEmail, 'new-email', array(
                'login' => Core_Auth::get('name'),
                'code_confirm' => $codeConfirm
            ));
            Core_Message::add('Активируйте новый email', Core_Message::TYPE_NOTICE);
        } else {
            $data['new_email'] = null;
        }
        $rolesTable->update($data, 'id = ' . Core_Auth::get('id'));
        Core_Auth::init();
        Core_Message::add('Данные сохранены.');
        return $this;        
    }
}
